<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'
import { deptTeacher } from '@/api/anniversary/statistics'

var dataAxis = []
var data = []
var chart = null

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '360px'
    }
  },
  data() {
    return {
      top: 40,
      yMax: 0,
      dataShadow: []
    }
  },
  mounted() {
    setTimeout(() => {
      deptTeacher({ top: this.top }).then(res => {
        if (res != null) {
          for (var i = 0; i < res.length; i++) {
            dataAxis.push(res[i].name)
            data.push(res[i].count)
          }
          this.yMax = Math.max(data) * 1.2

          for (var j = 0; j < data.length; j++) {
            this.dataShadow.push(this.yMax)
          }

          this.initChart()
          this.__resizeHandler = debounce(() => {
            if (chart) {
              chart.resize()
            }
          }, 100)
          window.addEventListener('resize', this.__resizeHandler)
        }
      }
      )
    }, 100)
  },
  beforeDestroy() {
    if (!chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    chart.dispose()
    chart = null
  },
  methods: {
    initChart() {
      chart = echarts.init(this.$el, 'macarons')

      chart.setOption({
        title: {
          text: '各学院教师校友分布'
        },
        tooltip: {
          trigger: 'axis',
          formatter: '{b} : {c}人',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        xAxis: {
          data: dataAxis,
          axisLabel: {
            rotate: -20,
            textStyle: {
              color: '#000'
            }
          },
          axisTick: {
            show: false
          },
          axisLine: {
            show: false
          },
          z: 10
        },
        yAxis: {
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            textStyle: {
              color: '#999'
            }
          }
        },
        dataZoom: [
          {
            type: 'inside'
          }
        ],
        series: [
          {
            type: 'bar',
            showBackground: true,
            itemStyle: {
              color: new echarts.graphic.LinearGradient(
                0, 0, 0, 1,
                [
                  { offset: 0, color: '#83bff6' },
                  { offset: 0.5, color: '#188df0' },
                  { offset: 1, color: '#188df0' }
                ]
              )
            },
            emphasis: {
              itemStyle: {
                color: new echarts.graphic.LinearGradient(
                  0, 0, 0, 1,
                  [
                    { offset: 0, color: '#2378f7' },
                    { offset: 0.7, color: '#2378f7' },
                    { offset: 1, color: '#83bff6' }
                  ]
                )
              }
            },
            data: data
          }
        ]
      })

      // Enable data zoom when user click bar.
      var zoomSize = 6
      chart.on('click', function(params) {
        // console.log(dataAxis[Math.max(params.dataIndex - zoomSize / 2, 0)]);
        chart.dispatchAction({
          type: 'dataZoom',
          startValue: dataAxis[Math.max(params.dataIndex - zoomSize / 2, 0)],
          endValue: dataAxis[Math.min(params.dataIndex + zoomSize / 2, data.length - 1)]
        })
      })
    }
  }
}
</script>
